package com.mall.controller.sys;


import com.mall.entity.sys.ProdtypeEO;
import com.mall.entity.sys.ResEO;
import com.mall.exception.MsgException;
import com.mall.service.sys.IProdtypeService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * <p>
 * 商品分类ID 前端控制器
 * </p>
 *
 * @author Dczzs
 * @since 2020-07-01
 */
@RestController
@RequestMapping("/sys/prodtype")
public class ProdtypeController {

	
	@Autowired
	private IProdtypeService service;
	
	
	
	@GetMapping("/list")
	public ResEO list() {
		ResEO res = new ResEO();
		
		List<ProdtypeEO> list = service.list();
		res.setData(list);
		
		
		return res;
		
	}
	
	@ApiOperation(value = "编辑商品分类",notes="根据用户ID查询当前商品分类信息")
	@ApiImplicitParam(paramType="path",name="id",value="用户ID",required=true)
	@GetMapping("/edit/{id}")
	public ResEO edit( @PathVariable Integer id) {
		ResEO res = new ResEO();
		
		try {
			ProdtypeEO eo =  service.getById(id);
			
			if(eo.getParentId() != null) {
			
				ProdtypeEO parentEO = service.getById(eo.getParentId());
				eo.setParentTypeName(parentEO.getTypeName());
			}
			res.setData(eo); 
		} catch (Exception e) {
			e.printStackTrace();
			res.setStatus(-1); 
		}
		
		
		return res;
		
	}
	
	@PostMapping("/save")
	public ResEO save(ProdtypeEO eo) {
	ResEO res = new ResEO();
	
		try {
			
			
			boolean saveOrUpdate = service.saveOrUpdate(eo);
			if(saveOrUpdate) {
				res.setMsg("已保存");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			res.setStatus(-1); 
		}
		return res;
	}
	
	@GetMapping("/delete/{id}")
	public ResEO delete(@PathVariable Integer id) {
		ResEO res = new ResEO();
		
		try {
			service.delete(id);
		} catch (MsgException e) {
			e.printStackTrace();
			res.setStatus(1);
		} catch (Exception e) {
			e.printStackTrace();
			res.setStatus(-1);
		}
		
		return res;
	}
	

}
